<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

const userInfo = reactive({
  username: '管理员',
  nickname: 'Admin',
  email: 'admin@example.com',
  phone: '13800138000',
  avatar: '👤',
  role: '超级管理员',
  createTime: '2023-01-01'
})

const isEditing = ref(false)
const formRef = ref(null)

const editForm = reactive({
  nickname: userInfo.nickname,
  email: userInfo.email,
  phone: userInfo.phone
})

const rules = {
  nickname: [
    { required: true, message: '请输入昵称', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
  ]
}

const handleEdit = () => {
  isEditing.value = true
  Object.assign(editForm, {
    nickname: userInfo.nickname,
    email: userInfo.email,
    phone: userInfo.phone
  })
}

const handleCancel = () => {
  isEditing.value = false
  formRef.value?.resetFields()
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    // 这里添加更新用户信息的API调用
    Object.assign(userInfo, {
      nickname: editForm.nickname,
      email: editForm.email,
      phone: editForm.phone
    })
    ElMessage.success('个人信息修改成功')
    isEditing.value = false
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}
</script>

<template>
  <div class="profile-container">
    <div class="profile-card">
      <div class="profile-header">
        <div class="avatar">{{ userInfo.avatar }}</div>
        <h2>个人信息</h2>
        <div class="header-actions" v-if="!isEditing">
          <el-button type="primary" @click="handleEdit">编辑</el-button>
        </div>
      </div>
      
      <!-- 查看模式 -->
      <div v-if="!isEditing" class="info-list">
        <div class="info-item">
          <span class="label">用户名：</span>
          <span class="value">{{ userInfo.username }}</span>
        </div>
        <div class="info-item">
          <span class="label">昵称：</span>
          <span class="value">{{ userInfo.nickname }}</span>
        </div>
        <div class="info-item">
          <span class="label">邮箱：</span>
          <span class="value">{{ userInfo.email }}</span>
        </div>
        <div class="info-item">
          <span class="label">手机：</span>
          <span class="value">{{ userInfo.phone }}</span>
        </div>
        <div class="info-item">
          <span class="label">角色：</span>
          <span class="value">{{ userInfo.role }}</span>
        </div>
        <div class="info-item">
          <span class="label">创建时间：</span>
          <span class="value">{{ userInfo.createTime }}</span>
        </div>
      </div>

      <!-- 编辑模式 -->
      <el-form
        v-else
        ref="formRef"
        :model="editForm"
        :rules="rules"
        label-width="100px"
        class="edit-form"
      >
        <el-form-item label="用户名">
          <el-input v-model="userInfo.username" disabled />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="editForm.nickname" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" />
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="editForm.phone" />
        </el-form-item>
        <el-form-item label="角色">
          <el-input v-model="userInfo.role" disabled />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">保存</el-button>
          <el-button @click="handleCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.profile-container {
  padding: 20px;
  min-height: calc(100vh - 60px);
  background: var(--bg-secondary);
}

.profile-card {
  background: var(--bg-primary);
  border-radius: 8px;
  box-shadow: var(--card-shadow);
  padding: 2rem;
  max-width: 800px;
  margin: 0 auto;
  border: 1px solid var(--border-color);
}

.profile-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border-color);
  position: relative;
}

.header-actions {
  position: absolute;
  right: 0;
}

.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--hover-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
}

h2 {
  color: var(--text-primary);
  margin: 0;
}

.info-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1.5rem;
}

.info-item {
  display: flex;
  align-items: center;
}

.label {
  color: var(--text-secondary);
  width: 100px;
  flex-shrink: 0;
}

.value {
  color: var(--text-primary);
}

.edit-form {
  max-width: 500px;
  margin: 0 auto;
}

:deep(.el-input__wrapper) {
  background-color: var(--bg-primary);
  box-shadow: 0 0 0 1px var(--border-color);
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--primary-color);
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px var(--primary-color);
}

:deep(.el-input.is-disabled .el-input__wrapper) {
  background-color: var(--bg-secondary);
  box-shadow: 0 0 0 1px var(--border-color);
}

:deep(.el-form-item__label) {
  color: var(--text-secondary);
}

:deep(.el-button) {
  margin-right: 1rem;
}
</style> 